﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.Provider
{
    public class UserStudyStatisticDAO : BaseDAO
    {
        public const string TableName = "view_UserStudyStatistic";

        #region 获取全部学员参与课程数和浏览量
        public static List<UserStudyStatisticInfo> GetAllModelsList()
        {
            var list = new List<UserStudyStatisticInfo>();

            string sqlString =
                $"SELECT UserId, UserName, Mobile, DisplayName, CoursewaresCount, TimesCount FROM {TableName} ORDER BY {nameof(UserStudyStatisticInfo.CoursewaresCount)} DESC, {nameof(UserStudyStatisticInfo.TimesCount)} DESC";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        #region 后台获取学员参与课程量排行榜
        public static List<UserStudyStatisticInfo> GetTopModelsList(int topNum)
        {
            var list = new List<UserStudyStatisticInfo>();

            string sqlString =
                $"SELECT TOP {topNum} UserId, UserName, Mobile, DisplayName, CoursewaresCount, TimesCount FROM {TableName} ORDER BY {nameof(UserStudyStatisticInfo.CoursewaresCount)} DESC";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        #region 获取指定学员参与课程量和浏览量
        private static UserStudyStatisticInfo GetModel(IDataRecord rdr)
        {
            if (rdr == null) return null;

            var _model = new UserStudyStatisticInfo();

            var i = 0;
            _model.UserId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.UserName = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.Mobile = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.Region = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.CoursewaresCount = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.TimesCount = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);

            return _model;
        }
        #endregion
    }
}